package cn.chencq.spider.novel.utils;

import java.io.IOException;
import java.io.InputStream;
import java.net.Proxy;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Utils {

	/**
	 * 获取书籍的状态
	 * 
	 * @param status
	 * @return
	 */
	public static int getBookStatus(String status) {
		if(status==null){
			return 0;
		}
		if ("连载".contains(status)) {
			return 1;
		} else if ("完结".contains(status) || "完成".contains(status)) {
			return 2;
		} else {
			return 0;// 未知
		}
	}

	public static Properties getProperties(String propertiesName) {
		InputStream inputStream = null;
		try {
			inputStream = Thread.currentThread().getContextClassLoader()
					.getResourceAsStream(propertiesName);

			Properties properties = new Properties();
			properties.load(inputStream);

			return properties;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			try {
				if (null != inputStream) {
					inputStream.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}

	public static Proxy getProxy() {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * 获取Host
	 * 
	 * @param url
	 * @return
	 */
	public static String getHost(String url) {
		String host = "";
		Pattern p = Pattern.compile("(?<=//|)((\\w)+\\.)+\\w+");
		Matcher matcher = p.matcher(url);
		if (matcher.find()) {
			host = matcher.group();
		}
		return host;
	}

	/**
	 * 获取顶级域名
	 * 
	 * @param url
	 * @return
	 */
	public static String getDomain(String url) {
		String domain = null;
		Pattern p = Pattern
				.compile("(?<=http://|\\.)[^.]*?\\.(com|cn|top|net|org|biz|info|cc|tv)");
		Matcher matcher = p.matcher(url);
		if (matcher.find()) {
			domain = matcher.group();
		}
		return domain;
	}
}
